package com.stedu.controller;

import com.github.pagehelper.PageInfo;
import com.stedu.bean.Employee;
import com.stedu.bean.RespBean;
import com.stedu.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/admin/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //分页查询, 查询所有
    @ResponseBody
    @RequestMapping( "/findByPage")
    public RespBean findByPage(Integer currentPage,String ename, String ejob) {

        //分页查询
        PageInfo pageInfo = employeeService.findByPage(currentPage, ename, ejob);

        //响应数据给前端
        return RespBean.ok("查询成功", pageInfo);
    }

    @ResponseBody
    @PostMapping
    public RespBean add(@RequestBody Employee e) throws Exception {

        employeeService.add(e);
        //响应信息给浏览器
        return RespBean.ok("添加成功");

    }

    @ResponseBody
    @DeleteMapping("/{eid}")
    public RespBean del(@PathVariable("eid") Integer eid) {

        employeeService.chgEstate(eid,0);
        return RespBean.ok("删除成功");
    }

    @ResponseBody
    @GetMapping("/{eid}")
    public RespBean findById(@PathVariable("eid") Integer eid) {

        Employee employee = employeeService.findById(eid);
        return RespBean.ok("查询成功",employee);

    }

    @ResponseBody
    @PutMapping
    public RespBean update(@RequestBody Employee e) {

        employeeService.update(e);
        //响应信息给浏览器
        return RespBean.ok("修改成功");

    }


}
